package middleware

import (
	"context"
	"go_ruoyi/global"
	"go_ruoyi/model/response_model"
	"go_ruoyi/utils"

	"github.com/gin-gonic/gin"
)

func Auth(c *gin.Context) {
	tokenString := c.GetHeader("Authorization")
	userMap, err := utils.ParseToken(tokenString)
	if err != nil {
		response_model.FailWithMsg(c, err.Error())
		c.Abort()
	}

	if global.Redis.Get(context.Background(), userMap["tokenId"].(string)).Val() == "" {
		response_model.FailWithMsg(c, "token已过期")
		c.Abort()
	}
	c.Set("userId", userMap["userId"])
	c.Set("userName", userMap["userName"])
	c.Set("tokenId", userMap["tokenId"])
	c.Next()
}
